package gutil

import (
	"sync"
)

type LinkMap struct {
	lock *sync.RWMutex
	sm   sync.Map
	m    map[string]interface{}
}

func NewLinkMap() *LinkMap {
	var lm LinkMap
	lm.Init()
	return &lm
}

func (c *LinkMap) Get(key string) interface{} {
	c.lock.RLock()
	if val, ok := c.m[key]; ok {
		c.lock.RUnlock()
		return val
	}
	c.lock.RUnlock()
	return nil
}
func (c *LinkMap) Add(key string, value interface{}) {
	c.lock.Lock()
	c.m[key] = value
	c.lock.Unlock()
}
func (c *LinkMap) Delete(key string) {
	c.lock.Lock()
	delete(c.m, key)
	c.lock.Unlock()
}

func (c *LinkMap) DeleteValue(key string, value interface{}) {
	c.lock.Lock()
	if c.m[key] == value {
		delete(c.m, key)
	}
	c.lock.Unlock()
}

func (c *LinkMap) TotalNum() int {
	c.lock.RLock()
	ln := len(c.m)
	c.lock.RUnlock()
	return ln
}
func (c *LinkMap) Init() {
	if c.m == nil {
		c.m = make(map[string]interface{})
		c.lock = new(sync.RWMutex)
	}
}
func (c *LinkMap) GetALl() map[string]interface{} {
	c.lock.RLock()
	cc := c.m
	c.lock.RUnlock()
	return cc
}
func (c *LinkMap) GetALLKey() []string {
	c.lock.RLock()
	data := make([]string, 0)
	for key, _ := range c.m {
		data = append(data, key)
	}
	c.lock.RUnlock()
	return data
}
